package com.server.server.strategy;

import com.server.exception.BusinessException;
import com.server.model.enums.ExceptionInfo;
import com.server.server.message.enums.SerializeAlgorithm;
import com.server.server.message.serialize.HessianSerialization;
import com.server.server.message.serialize.JsonSerialization;
import com.server.server.message.serialize.Serializer;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description: This is a sample class.
 * @Author: Simon
 */
public class SerializeAlgorithmStrategySelector {

    private final Map<SerializeAlgorithm, Serializer> serializerMap=new ConcurrentHashMap<>();

    public SerializeAlgorithmStrategySelector() {
        serializerMap.put(SerializeAlgorithm.JSON, new JsonSerialization());
        serializerMap.put(SerializeAlgorithm.HESSIAN, new HessianSerialization());

    }

    public Serializer selectStrategy(SerializeAlgorithm typeEnum) {
        Serializer strategy = serializerMap.get(typeEnum);
        if (strategy == null) {
            throw new BusinessException(ExceptionInfo.SERIALIZE_ALGORITHM_UNSUPPORTED);
        }
        return strategy;
    }
}
